<?php
namespace HelloAdmin\Controller;

class IndexController extends CommonController {

    public function index(){
        $this->display();
    }

    public function index_v3(){
        $d = date('d',time(oid));
        $m = date('m',time(oid));
        $y = date('Y',time(oid));
        $where = "`year`='{$y}' AND `month`='{$m}' AND `day`='{$d}'";
        $bookTnum = D('Common/Book')->where($where)->count('book_id');
        $bookTotalNum = D('Common/Book')->count('book_id');
        $articleNewNum = D('Common/BookArticle')->where($where)->count('article_id');
        $memberTotalNum = D('Common/Members')->count('member_id');
        $articleTotalNum = D('Common/BookArticle')->count('article_id');
        $baiduTop = D('Common/Setting')->getValue('baiduTop');

        $this->assign('baiduTop',explode(':',$baiduTop));
        $this->assign('articleTotalNum',$articleTotalNum);
        $this->assign('bookNnum',$bookTnum);
        $this->assign('bookTotalNum',$bookTotalNum);
        $this->assign('articleNewNum',$articleNewNum);
        $this->assign('memberTotalNum',$memberTotalNum);

        $messageErrorNum = D('Common/Message')->where("`status`=1 AND `type`='err'")->select();
        $this->assign('messageError',$messageErrorNum);

        $crontab = D('Common/Crontab')->order("`id` DESC")->limit(20)->select();
        $this->assign('crontab',$crontab);
        $this->display();
    }

    public function out() {
        if(session('user_id') > 0) {
            session(C('USER_AUTH_KEY'),NULL);
            session('user_id',0);
            session_destroy();
        }
        $this->redirect('Public/index');
    }

    //公告
    public function gonggaopai(){

      $this->display();
    }
    public function addNotice(){
        if(empty($_POST['disabled'])){
            $this->display();exit;
        }
        $Link = new \Common\Model\NoticeModel();
        $_POST['ctime'] = time();
        $Link->create($_POST);
        if($Link->add()){
            $this->layerIfreamRe('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function gonggaopaiData(){
        $search = I('post.search');
        $field = I('post.field');
        $d_a = I('post.order');
        if(empty($search)){
           $where = '1=1';
        }else{
            $where = "`name` like '{$search}%'";
        }
        $order = '';
        if(!empty($field) && $d_a){
            $order = "`{$field}` $d_a";
        }

        $Link = new \Common\Model\NoticeModel();
        $data = $Link->getList($where,$order,$this->offset,$this->limit);
        echo (json_encode($data));
    }

    public function updateLink(){
        $Link = new \Common\Model\LinkModel();
        if($Link->updateField()){
            echo 'success';exit;
        }
    }
    public function updateNotice(){
        $Link = new \Common\Model\NoticeModel();
        if($Link->updateField()){
            echo 'success';exit;
        }
    }

    //基础设置
    public function basic(){

        $this->assign('indexTj',\Common\Model\SettingModel::getValue('index-tj'));
        $this->assign('jindTj',\Common\Model\SettingModel::getValue('jind-tj'));
        $this->assign('hotTj',\Common\Model\SettingModel::getValue('hot-tj'));
        $this->display();
    }

    public function ipList(){
        $this->assign('ipDentrt',\Common\Model\SettingModel::getValue('ipDentrt'));
        $this->assign('ipAllow',\Common\Model\SettingModel::getValue('ipAllow'));
        $this->display();
    }

    public function updateSetting(){
      $name = I('post.name');
      $value = I('post.value');
      if($name && $value){
          $data = array('name'=>$name,'value'=>$value);
          if(D('Common/Setting')->saveValue($data)){
            $this->layerIfreamRe();
          }
      }
      $this->layerIfreamRe('','s','error');
    }


    public function addLink(){
        if(empty($_POST['disabled'])){
            $this->display('addLink');exit;
        }
        $Link = new \Common\Model\LinkModel();
        $_POST['create_time'] = time();
        $Link->create($_POST);
        if($Link->add()){
            $this->layerIfreamRe('添加成功');
        }else{
            $this->error('添加失败');
        }

    }

        //友情链接数据
    public function friendLinkData(){
        $search = I('post.search');
        $field = I('post.field');
        $d_a = I('post.order');
        if(empty($search)){
           $where = '1=1';
        }else{
            $where = "`name` like '{$search}%'";
        }
        $order = '';
        if(!empty($field) && $d_a){
            $order = "`{$field}` $d_a";
        }

        $Link = new \Common\Model\LinkModel();
        $data = $Link->getList($where,$order,$this->offset,$this->limit);
        echo (json_encode($data));
    }

    public function delLink(){
        $ids = I('post.ids');
        if(empty($ids)){
            exit('error');
        }
        $Link = new \Common\Model\LinkModel();
        if($Link->delLink($ids)){
            exit('success');
        }
    }

    public function clearCa(){
        $do = I('post.do');
        switch ($do) {
            case 'runtimelog':
                if(delDir('./Runtime/Logs/')){
                    exit('success');
                }
                break;
            case 'runtimecache':
                if(delDir('./Runtime/Cache/')){
                    exit('success');
                }
                break;
            case 'colletlog':
                if(delDir('./Runtime/Collect/')){
                    exit('success');
                }
                break;
            case 'caches':
                if(delDir('./Runtime/Temp/')){
                    exit('success');
                }
                break;
            case 'runtimedata':
                if(delDir('./Runtime/Data/')){
                    exit('success');
                }
                break;
            case 'cachehtml':
                if(delDir('./Application/Html/')){
                    exit('success');
                }
                break;
            case 'mysqlOPTIMIZE':
                    $sql = 'OPTIMIZE TABLE `sdb_book`, `sdb_book_article`, `sdb_book_author`, `sdb_book_cate`, `sdb_book_content`, `sdb_book_shelf`, `sdb_book_tj`, `sdb_crontab`, `sdb_image`, `sdb_label`, `sdb_link`, `sdb_members`, `sdb_member_lv`, `sdb_member_msg`, `sdb_member_pam`, `sdb_member_point`, `sdb_message`, `sdb_node`, `sdb_notice`, `sdb_operatorlog_logs`, `sdb_read_log`, `sdb_role`, `sdb_role_node`, `sdb_role_user`, `sdb_setting`, `sdb_share_log`, `sdb_user`';
                    M()->execute($sql);
                    exit('success');
                break;
            case 'mysqlREPAIR':
                    $sql = 'REPAIR TABLE `sdb_book`, `sdb_book_article`, `sdb_book_author`, `sdb_book_cate`, `sdb_book_content`, `sdb_book_shelf`, `sdb_book_tj`, `sdb_crontab`, `sdb_image`, `sdb_label`, `sdb_link`, `sdb_members`, `sdb_member_lv`, `sdb_member_msg`, `sdb_member_pam`, `sdb_member_point`, `sdb_message`, `sdb_node`, `sdb_notice`, `sdb_operatorlog_logs`, `sdb_read_log`, `sdb_role`, `sdb_role_node`, `sdb_role_user`, `sdb_setting`, `sdb_share_log`, `sdb_user`';
                    M()->execute($sql);
                    exit('success');
                break;
        }


    }

    public function createMap(){
        $bookXmlPath = 'book_1.xml';
        $data = D('Common/Book')->where('disabled=1')->field('book_id,create_time')->limit(24500)->select();
        $str = '<?xml version="1.0" encoding="utf-8"?><urlset>';
        $str .='<url>';
        $str .= '<loc>http://896zw.com/index.html</loc>';
        $str .= '<lastmod>'.date('Y-m-d',time()).'</lastmod>';
        $str .= '<changefreq>daily</changefreq>';
        $str .= '<priority>1</priority>';
        $str .= '</url>';
        foreach ($data as $key => $value) {
            $str .='<url>';
            $str .= '<loc>'.'http://896zw.com/'.$value['book_id'].'.html'.'</loc>';
            $str .= '<lastmod>'.date('Y-m-d',$value['create_time']).'</lastmod>';
            $str .= '<changefreq>daily</changefreq>';
            $str .= '<priority>0.8</priority>';
            $str .='</url>';
        }
        $str .= '</urlset>';
        file_put_contents('./'.$bookXmlPath, $str);
        unset($str,$data);
        $map = '<?xml version="1.0" encoding="UTF-8"?><sitemapindex><sitemap><loc>http://www.896zw.com/'.$bookXmlPath.'</loc><lastmod>'.date('Y-m-d',time()).'</lastmod></sitemap>';
        $Article = new \Common\Model\BookArticleModel();
        $count = $Article->count('article_id');
        $count = ceil($count/24500);
        for($i=0;$i<$count;$i++){
            $offset = $i*24500;
            $data = $Article->field('book_id,create_time,article_id')->limit($offset,24500)->select();
            $str = '<?xml version="1.0" encoding="utf-8"?><urlset>';
            foreach ($data as $k => $v) {
                $str .='<url>';
                $str .= '<loc>'.'http://896zw.com/'.$v['book_id'].'-'.$v['article_id'].'.html'.'</loc>';
                $str .= '<lastmod>'.date('Y-m-d',$v['create_time']).'</lastmod>';
                $str .= '<changefreq>never</changefreq>';
                $str .= '<priority>0.6</priority>';
                $str .='</url>';
            }
            $str .= '</urlset>';
            $articleXmlPath = 'article_'.$i.'.xml';
            file_put_contents('./'.$articleXmlPath, $str);
            unset($str,$data);
            $map .= '<sitemap>';
            $map .= '<loc>http://896zw.com/'.$articleXmlPath.'</loc>';
            $map .= '<lastmod>'.date('Y-m-d',time()).'</lastmod>';
            $map .= '</sitemap>';
        }
        $map .= '</sitemapindex>';
        file_put_contents('./sitemap_896zw.xml', $map);
        exit('success');

    }


}